<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        //创建lol英雄对象
        //思考用什么方式创建:1.原始模式，2.构造函数，3.混合模式   
        //1.公共方法和属性， 
        /*1.0 通过构造函数创建英雄对象，公共方法放在对象原型上面*/
        function Hero(heroData){
            // this.name = heroData.name
            // this.hp = heroData.hp
            // this.mp = heroData.mp
            // this.skill = heroData.skill
            // this.position = heroData.position
            for(key in heroData){
                this[key] = heroData[key]
            }
            this._hp = heroData.hp
            //设置对象的属性
            Object.defineProperties(this,{
                hp:{
                    //当获取对象的hp即ms.hp时候调用此函数
                    get:function(){
                        return this._hp
                    },
                    //当设置对象的hp即ms.hp = 10,的时候调用此函数
                    set:function(value){
                        if(value<0){
                            this._hp = 0
                        }else{
                            this._hp = value
                        }
                    }
                }
            })
        }
        //公共方法放在原型上面
        Hero.prototype.attack = function(otherHero){
            if(otherHero.hp <= 0){
                console.log(`${otherHero.name}已经阵亡，请不要鞭尸`)
                return
            }
            this.say()
            otherHero.hp -= this.gjl
            var s1 = `${this.name}打了${otherHero.name}一下，${otherHero.name}损失了${this.gjl}`
            console.log(s1)
        }

        Hero.prototype.say = function(){
            function randomChoice(arr){
                let randomIndex = Math.round(Math.random()*(arr.length-1))
                return arr[randomIndex]
            }
            let curKh = randomChoice(this.ks)
            console.log(curKh)
        }

        var ysData = {
            name:"亚索",
            nc:"快乐风男",
            ks:['哈萨给','死亡如风，常伴吾生'],
            hp:100,
            mp:0,
            gjl:60,
            skill:['q','w'],
            position:['中路']
        }
        var xzData = {
            name:"李青",
            nc:"瞎子",
            ks:['衣裤','我用双手成就你的梦想'],
            hp:120,
            mp:0,
            gjl:70,
            skill:["q",'w'],
            position:['大野']
        }

        var zxData = {
            name:"赵新",
            nc:"菊花信",
            ks:['长枪依在，菊花拿来','万军丛中取敌将首级'],
            hp:120,
            mp:0,
            gjl:75,
            skill:["q",'w'],
            position:['大野']
        }

        
   

        let ys = new Hero(ysData)
        let ms = new Hero(xzData)
        let zx = new Hero(zxData)

        ys.attack(zx)
    </script>
</body>
</html>